package com.shanghaionstar.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.nav.share.ShareImgDialog;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Longmin Li on 2017/6/21.
 * Url转Bitmap
 */

public class UrlToBitmapTask extends AsyncTask {
    CommonWebViewActivity mContext;
    private String imgUrl;

    public UrlToBitmapTask(CommonWebViewActivity mContext, String imgUrl) {
        this.mContext = mContext;
        this.imgUrl = imgUrl;
    }

    @Override
    protected Bitmap doInBackground(Object[] params) {
        URL fileUrl = null;
        Bitmap bitmap = null;

        try {
            fileUrl = new URL(imgUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) fileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        showShareDialog((Bitmap) o);
    }

    private void showShareDialog(Bitmap bitmap) {
//        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.DiagnosticsShare.getStringValue());
        ShareImgDialog dialog = new ShareImgDialog(mContext, R.style.Theme_Transparent);
        dialog.setShareBitmap(bitmap);
//        dialog.setReportCancel(FunctionNameFindId.DiagShare_Cancel.getStringValue());
//        dialog.setMoments(FunctionNameFindId.DiagShare_Moments.getStringValue());
//        dialog.setWechat(FunctionNameFindId.DiagShare_Wechat.getStringValue());
        dialog.show();
    }
}
